/**  
 * @Title: ModelServiceImpl.java
 * @Package com.xzit.tmms.service.task.impl
 * @Description: TODO
 * @author 董亮亮
 * @date 2016年6月27日 上午8:56:48
 * @version V1.0  
 */
package com.xzit.tmms.service.task.impl;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xzit.tmms.bean.task.Meterial;
import com.xzit.tmms.bean.task.Model;
import com.xzit.tmms.common.exception.ServiceException;
import com.xzit.tmms.common.page.Page;
import com.xzit.tmms.common.util.CommonUtil;
import com.xzit.tmms.mapper.task.MeterialMapper;
import com.xzit.tmms.mapper.task.ModelMapper;
import com.xzit.tmms.mapper.task.TaskMapper;
import com.xzit.tmms.service.task.ModelService;

/**
 * 
 * @ClassName: ModelServiceImpl
 * @Description: TODO
 * @author ChongQin
 * @date 2016年7月2日 上午10:05:13
 */
@Service("modelService")
public class ModelServiceImpl implements ModelService {

	@Resource
	private ModelMapper modelMapper;

	@Resource
	private MeterialMapper meterialMapper;

	@Resource
	private TaskMapper taskMapper;

	@Override
	public void addModel(Model model) throws ServiceException {
		try {
			modelMapper.save(model);
			// 获取模板信息
			String meteName = model.getModelName();
			String meteType = model.getModelType();
			Integer modelId = model.getModelId();
			// 更新现有任务的材料表
			for (Integer taskId : taskMapper.getAllTaskIds()) {
				Meterial meterial = new Meterial();
				meterial.setMeteName(meteName);
				meterial.setMeteType(meteType);
				meterial.setModelId(modelId);
				meterial.setTaskId(taskId);
				// 为每一个task添加材料记录
				meterialMapper.save(meterial);
			}
		} catch (Exception e) {
			throw new ServiceException("保存模板失败");
		}
	}

	@Override
	public void deleteModel(Integer modelId) throws ServiceException {
		try {
			modelMapper.delete(modelId);
		} catch (Exception e) {
			throw new ServiceException(e);
		}

	}

	public Page<Map<String, Object>> queryModel(Page<Map<String, Object>> page) throws ServiceException {
		try {
			modelMapper.queryModel(page);
		} catch (Exception e) {
			throw new ServiceException("查询模板异常");
		}
		return page;
	}

	@Override
	public void updateModel(Model model) throws ServiceException {
		try {
			// id校验
			Integer modelId = model.getModelId();
			if (CommonUtil.isNotEmpty(modelId)) {
				modelMapper.update(model);
				// 检查model Name 是否改变
				String meteName = model.getModelName();
				if (CommonUtil.isNotEmpty(meteName)) {
					// 修改相应材料的name
					for (Integer meteId : meterialMapper.getMeteIdsByModelId(modelId)) {
						Meterial meterial = new Meterial();
						meterial.setMeteId(meteId);
						meterial.setMeteName(meteName);
						// 更新meterial的name
						meterialMapper.update(meterial);
					}
				}
			}
		} catch (Exception e) {
			throw new ServiceException(e);
		}

	}

	@Override
	public Model getModelById(Integer modelId) throws ServiceException {
		try {
			return modelMapper.getById(modelId);
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

}
